Ext.namespace("DSYGrid.component");
/**
 * 创建DSYWidget管理器
 */
if (!window.DSYWidget) {
    window.DSYWidget = {
        Manager: {
            grid: {}
        }
    };
    window.DSYWidget = DSYWidget;
} else if (!window.DSYWidget.Manager) {
    window.DSYWidget.Manager = {
        grid: {}
    };
} else if (!window.DSYWidget.Manager.grid) {
    window.DSYWidget.Manager.grid = {};
}
/**
 * 创建表格封装对象
 *
 * @returns {DSYGrid}
 */
function DSYGrid() {
    this.itemId = null;// gridid，可根据该id获取配置信息
    this.renderTo = null; // 渲染Grid的容器
    this.title = null;
    this.width = null; // 宽度
    this.height = null; // 高度
    this.autoLoad = true;//是否立即加载
    this.columnAutoWidth = true;// 列宽自适应
    this.rowCss = null;//行样式css
    var headerConfig = {
        headerJson: null,        // 表头配置源
        headerUrl: null,         // 表头json
        columnAutoWidth: true,  // 列宽自适应
        headerCss: 'background-color: #6AB5EE;color: #fff;text-align: center;',// 表头默认样式
        headerCssOver: 'background-color: #eef6fb;color: #6AB5EE;'// 表头默认样式：鼠标悬浮
    };
    this.headerJson = null;// 表头
    this.headerUrl = null; // Grid表头配置源
    this.headerCss = null;// 表头配置css
    this.headerCssOver = null;// 表头配置css，鼠标悬浮
    this.data = null; //Grid数据集,当数据集不存在时，再去查找数据源地址
    this.dataUrl = null; // Grid数据源地址
    this.columns = [];// 列模型
    this.columnLines = true;// 表格线
    this.fields = [];// 解析数据的字段
    this.border = true;//是否显示边界，默认true显示
    this.checkBox = false;// 是否显示复选框，默认不显示
    this.selModel = {
        selType: 'rowmodel',//选择模式，主要有cellmodel/rowmodel/checkboxmodel
        mode: "MULTI"     //s是否多选，默认多选,"SINGLE"/"SIMPLE"/"MULTI"
    };
    this.tbar = [];//顶部工具栏
    this.tbarHeight = null;//顶部工具栏高度
    this.bbar = [];//底部工具栏
    this.bbarHeight = null;//底部工具栏高度
    this.rbar = [];//右侧工具栏
    this.rbarHeight = null;//右侧工具栏高度
    this.plugins = [];//插件
    this.features = [];//统计插件
    //分页相关配置
    var pageConfig = {
        enablePage: true,// 是否分页，默认分页
        pageSize: 10,// 每页显示数据数
        pageNum: false,//是否显示每页条数
        pageNumStyle: 'combo',//显示样式combo/numberfield
        pageNumWidth: 60,//每页显示记录数宽度
        pageNumIndex: 0,//每页条数显示位置
        pageToolBar: null//分页按钮,表格创建后放置分页按钮
    };
    //行号相关配置
    var rowNumber = {
        rowNumber: false,// 是否显示行号，默认不显示
        header: null,// 行号标题
        width: 33
    };
    //导出功能配置
    var exportExcel = {
        exportExcel: false,//是否导出，默认false，不导出
        url: null,//导出按钮URL
        param: {},//导出参数
        btn: null,//自定义导出按钮，默认为null
        //给导出按钮绑定事件
        on: function (eventName, event) {
            this.listener[eventName] = event;
            //当按钮存在,并且不是beforeClick和click事件时，给按钮绑定事件
            if (typeof(this.id) != "undefined" && this.id != null && this.btn != null && eventName != "beforeClick" && eventName != "click") {
                this.btn.on(eventName, event);
            }
        },
        //设置URL
        setURL: function (url) {
            this.url = url;
        },
        listener: {}
    };
    this.params = {};//发送ajax请求数据源时传递的参数
    this.config = null;//配置JSON串
    this.grid = null;//获取创建的表格

    // set、get方法
    this.getRenderTo = function () {
        return this.renderTo;
    };
    this.setRenderTo = function (renderTo) {
        this.renderTo = renderTo;
    };
    this.getItemId = function () {
        return this.itemId;
    };
    this.setItemId = function (itemId) {
        this.itemId = itemId;
    };
    this.getTitle = function () {
        return this.title;
    };
    this.setTitle = function (title) {
        this.title = title;
    };
    this.getWidth = function () {
        return this.width;
    };
    this.setWidth = function (width) {
        this.width = width;
    };
    this.getHeight = function () {
        return this.height;
    };
    this.setHeight = function (height) {
        this.height = height;
    };
    this.getAutoLoad = function () {
        return this.autoLoad;
    };
    this.setAutoLoad = function (autoLoad) {
        this.autoLoad = autoLoad;
    };
    this.getColumnAutoWidth = function () {
        return this.columnAutoWidth;
    };
    this.setColumnAutoWidth = function (columnAutoWidth) {
        this.columnAutoWidth = columnAutoWidth;
    };
    this.getRowCss = function () {
        return this.rowCss;
    };
    this.setRowCss = function (rowCss) {
        this.rowCss = rowCss;
    };
    this.getHeaderConfig = function () {
        return headerConfig;
    };
    this.setHeaderConfig = function (_headerConfig) {
        jQuery.extend(true, headerConfig, _headerConfig);
    };
    this.getHeaderJson = function () {
        return this.headerJson;
    };
    this.setHeaderJson = function (headerJson) {
        this.headerJson = headerJson;
    };
    this.getHeaderCss = function () {
        return this.headerCss;
    };
    this.setHeaderCss = function (headerCss) {
        this.headerCss = headerCss;
    };
    this.getHeaderCssOver = function () {
        return this.headerCssOver;
    };
    this.setHeaderCssOver = function (headerCssOver) {
        this.headerCssOver = headerCssOver;
    };
    this.getHeaderUrl = function () {
        return this.headerUrl;
    };
    this.setHeaderUrl = function (headerUrl) {
        this.headerUrl = headerUrl;
    };
    this.getData = function () {
        return this.data;
    };
    this.setData = function (data) {
        this.data = data;
    };
    this.getDataUrl = function () {
        return this.dataUrl;
    };
    this.setDataUrl = function (dataUrl) {
        this.dataUrl = dataUrl;
    };
    this.getColumns = function () {
        return this.columns;
    };
    this.setColumns = function (columns) {
        this.columns = columns;
    };
    this.getFields = function () {
        return this.fields;
    };
    this.setFields = function (fields) {
        this.fields = fields;
    };
    this.getColumnLines = function () {
        return this.columnLines;
    };
    this.setColumnLines = function (columnLines) {
        this.columnLines = columnLines;
    };
    this.getBorder = function () {
        return this.border;
    };
    this.setBorder = function (border) {
        this.border = border;
    };
    this.getCheckBox = function () {
        return this.checkBox;
    };
    this.setCheckBox = function (checkBox) {
        this.checkBox = checkBox;
    };
    this.getSelModel = function () {
        return this.selModel;
    };
    this.setSelModel = function (selModel) {
        this.selModel = selModel;
    };
    this.getBbar = function () {
        return this.bbar;
    };
    this.setBbar = function (bbar) {
        this.bbar = bbar;
    };
    this.getBbarHeight = function () {
        return this.bbarHeight;
    };
    this.setBbarHeight = function (bbarHeight) {
        this.bbarHeight = bbarHeight;
    };

    this.getRbar = function () {
        return this.rbar;
    };
    this.setRbar = function (rbar) {
        this.rbar = rbar;
    };
    this.getRbarHeight = function () {
        return this.rbarHeight;
    };
    this.setRbarHeight = function (rbarHeight) {
        this.rbarHeight = rbarHeight;
    };

    this.getTbar = function () {
        return this.tbar;
    };
    this.setTbar = function (tbar) {
        this.tbar = tbar;
    };
    this.getTbarHeight = function () {
        return this.tbarHeight;
    };
    this.setTbarHeight = function (tbarHeight) {
        this.tbarHeight = tbarHeight;
    };
    this.getPageConfig = function () {
        return pageConfig;
    };
    this.setPageConfig = function (_pageConfig) {
        if (typeof(_pageConfig) == "boolean") {
            pageConfig.enablePage = _pageConfig;
        } else if (typeof(_pageConfig) == "object") {
            jQuery.extend(true, pageConfig, _pageConfig);
        }
    };
    this.getRowNumber = function () {
        return rowNumber;
    };
    this.setRowNumber = function (_rowNumber) {
        if (typeof(_rowNumber) == "boolean") {
            rowNumber.rowNumber = _rowNumber;
        } else if (typeof(_rowNumber) == "object") {
            jQuery.extend(true, rowNumber, _rowNumber);
        }
    };
    this.getExportExcel = function () {
        return exportExcel;
    };
    this.setExportExcel = function (_exportExcel, url) {
        if (typeof(_exportExcel) == "boolean") {
            exportExcel.exportExcel = _exportExcel;
            exportExcel.url = url;
        } else if (typeof(_exportExcel) == "object") {
            jQuery.extend(true, exportExcel, _exportExcel);
        }
    };
    this.getParams = function () {
        return this.params;
    };
    this.setParams = function (params) {
        this.params = params;
    };
    this.getConfig = function () {
        return this.config;
    };
    this.setConfig = function (config) {
        this.config = config;
    };
    this.getGrid = function () {
        return this.grid;
    };
    this.setGrid = function (grid) {
        this.grid = grid;
    };
    this.setPlugins = function (plugins) {
        this.plugins = plugins;
    };
    this.getPlugins = function () {
        return plugins;
    };
    this.setFeatures = function (features) {
        this.features = features;
    };
    this.getFeatures = function () {
        return features;
    };

    /**
     * 导出按钮配置
     */
    /*this.exportExcelConfig = function () {
        // 如果导出功能为true
        if (exportExcel.exportExcel) {
            //将表头css转换为对象
            var headerCssArray = this.headerCss.split(";");
            var headerCss = {};
            for (var headerCssArray_seq in headerCssArray) {
                var name = headerCssArray[headerCssArray_seq].split(":")[0];
                if (typeof(name) == "undefined") {
                    continue;
                }
                name = name.trim();
                var value = headerCssArray[headerCssArray_seq].split(":")[1];
                if (typeof(value) == "undefined") {
                    continue;
                }
                value = value.trim();
                headerCss[name] = value;
            }
            var v_colArray = this.getHeaderJson().header;
            //获取各列列宽
            var columns = this.grid.getColumns();
            for (var v_colArray_seq in v_colArray) {
                var seq = parseInt(v_colArray_seq, 10);
                if (rowNumber.rowNumber) {
                    //如果显示行号
                    seq++;
                }
                v_colArray[v_colArray_seq].width = columns[seq].getCellWidth();
            }
            //获取各列css
            for (var v_colArray_seq in v_colArray) {
                if (typeof(v_colArray[v_colArray_seq].css) == "undefined" || v_colArray[v_colArray_seq].css == null || v_colArray[v_colArray_seq].css == "") {
                    continue;
                }
                //将表头css转换为对象
                var colCssArray = v_colArray[v_colArray_seq].css.split(";");
                var colCss = {};
                for (var colCssArray_seq in colCssArray) {
                    var name = colCssArray[colCssArray_seq].split(":")[0];
                    if (typeof(name) == "undefined") {
                        continue;
                    }
                    name = name.trim();
                    var value = colCssArray[colCssArray_seq].split(":")[1];
                    if (typeof(value) == "undefined") {
                        continue;
                    }
                    value = value.trim();
                    colCss[name] = value;
                }
                v_colArray[v_colArray_seq].css = colCss;
            }
            //配置JSON
            exportExcel.config = {
                header: v_colArray,
                config: {
                    headerCss: headerCss
                }
            };
            //如果按钮为自定义的字符串，将其置为ext获取的元素
            if (typeof(exportExcel.btn) == "string") {
                exportExcel.btn = Ext.get(exportExcel.id);
            } else {
                //如果按钮不为自定义的字符串，重新设置id
            }

        }
    };*/
    /**
     * 导出按钮绑定事件
     */
    this.exportExcelBind = function () {
        // 如果导出功能为true,并且导出按钮存在
        if (exportExcel.exportExcel && typeof(exportExcel.id) != "undefined" && exportExcel.id != null && exportExcel.btn != null) {
            //当不是beforeClick和click事件时，给按钮绑定事件
            for (var i in exportExcel.listener) {
                if (i != "beforeClick" && i != "click") {
                    exportExcel.btn.on(i, exportExcel.listener[i]);
                }
            }
            //给导出按钮添加点击事件
            exportExcel.btn.on("click", function (btn) {
                //获取用户绑定的beforeClick事件和click事件
                var beforeClick;
                var click;
                for (var i in exportExcel.listener) {
                    if (i == "beforeClick") {
                        beforeClick = exportExcel.listener[i];
                    } else if (i == "click") {
                        click = exportExcel.listener[i];
                    }
                }
                //执行beforeClick事件
                if (typeof(beforeClick) == "function") {
                    var flag = beforeClick();
                    if (!flag) {
                        return;
                    }
                }
                //执行click事件
                if (typeof(click) == "function") {
                    click();
                }
                var config = JSON.stringify(exportExcel.config);
                var param = $.extend(exportExcel.param, btn.up('grid').getStore().getProxy().extraParams);
                exportExcelFunc(exportExcel.url, param, config);
            });
        }
    };
    /**
     * 递归数组：获取列解析模型field
     * @param array 表头配置数组
     */
    this.setGridFields = function (array) {
        //如果为空返回
        if (array == null || array.length <= 0) {
            return;
        }
        for (var array_seq in array) {
            if (array[array_seq].dataIndex && array[array_seq].dataIndex != '') {
                this.fields.push({
                    name: array[array_seq].dataIndex,// 设置列数据字段
                    type: array[array_seq].type == null ? "auto" : array[array_seq].type // 设置列解析类型
                });
            }
            if (array[array_seq].columns && Ext.isArray(array[array_seq].columns)) {
                this.setGridFields(array[array_seq].columns);
            }
        }
    }
    /**
     * 根据配置数组单列，生成对应的单列配置
     * @param col
     * @returns {*}
     */
    this.setGridColumn = function (col) {
        var column = (col);
        if (col.text) {
            column.header = col.text;// 设置标题文字
            delete column.text;
        }
        column.hidden = col.hidden ? col.hidden : false;// 设置列隐藏
        //设置宽度默认值
        if (typeof (col.width) == 'undefined' || col.width == null) {
            col.width = 100;
        }
        //判断列为自适应还是固定宽度
        if (this.columnAutoWidth) {
            column.flex = col.width ? col.width : 100;// 设置列比例;
            delete column.width;
        } else {
            column.width = col.width ? col.width : 100;// 设置固定列宽;

        }
        // 默认列渲染类型为字符型
        // 设置列渲染类型为数字型，右对齐
        if (col.type == "int") {
            column.align = "right";
            column.xtype = "numbercolumn";
            column.format = "0";
        } else if (col.type == "float") {
            column.align = "right";
            column.xtype = "numbercolumn";
        }
        if (col.xtype != null) {
            column.xtype = col.xtype;
        }
        //设置列对齐方式
        if (col.align != null && col.align != "") {
            column.align = col.align;
        }
        // 设置列渲染格式化
        if (col.format != null && col.format != "") {
            column.format = col.format;
        }
        //设置是否提示
        if (col.tipable) {
            column.renderer = (function () {
                return function (data, cell) {
                    cell.tdAttr = 'data-qtip="' + data + '"';
                    return data;
                };
            })();
        }
        // 设置列的字体和颜色
        if (col.css != null) {
            column.css = col.css;
            if (col.tipable) {
                column.renderer = (function () {
                    var css = col.css;
                    return function (data, cell) {
                        //设置单元格内容提示
                        cell.tdAttr = 'data-qtip="' + data + '"';
                        return '<span style="' + css + '">' + data + '</span>';
                    };
                })();
            } else {
                column.renderer = (function () {
                    var css = col.css;
                    return function (data) {
                        return '<span style="' + css + '">' + data + '</span>';
                    };
                })();
            }
        }
        return column;
    }
    /**
     * 递归数组：获取列渲染模型columns
     * @param array 表头配置数组
     */
    this.setGridColumns = function (array) {
        //如果为空返回
        if (array == null || array.length <= 0) {
            return;
        }
        var columns = [];
        for (var array_seq in array) {
            var column = {};
            column = this.setGridColumn(array[array_seq]);
            if (array[array_seq].columns && Ext.isArray(array[array_seq].columns)) {
                column.columns = this.setGridColumns(array[array_seq].columns);
            }
            columns.push(column);
        }
        return columns;
    };
    /**
     * 配置表格,初始化
     */
    this.loadProperty = function () {
        var itemId = this.itemId;
        var headerJson = this.headerJson;
        var headerURL = this.headerUrl;
        // 设置表头样式和表头悬浮样式
        if (this.headerCss == null) {
            this.headerCss = "background-color: #6AB5EE;color: #fff;text-align: center;";
        } else {
            this.headerCss = "background-color: #6AB5EE;color: #fff;text-align: center;" + this.headerCss;
        }
        $('<style>.x-column-header-inner{' + this.headerCss + '}</style>').appendTo(document.body);
        if (this.headerCssOver == null) {
            this.headerCssOver = "background-color: #eef6fb;color: #6AB5EE;";
        } else {
            this.headerCssOver = "background-color: #eef6fb;color: #6AB5EE;" + this.headerCssOver;
        }
        $('<style>.x-column-header-over{' + this.headerCssOver + '}</style>').appendTo(document.body);

        // 将表格列配置转换为JSON
        var v_colArray = null;// 表头JSON数组
        if (headerJson != null && headerURL != "" && headerURL != {}) {
            v_colArray = headerJson.header;
        } else if (headerURL != null && headerURL != "") {
            // 如果表头配置源不为空，就Ajax请求获取配置源
            Ext.Ajax.request({
                method: 'POST',
                url: this.headerUrl,
                params: {
                    itemId: itemId
                },
                async: false,
                success: function (response) {
                    // 获取返回的JSON，并根据itemId，获取表头配置信息
                    var respText = Ext.util.JSON.decode(response.responseText);
                    v_colArray = eval(respText.header);
                },
                failure: function () {
                    Ext.MessageBox.minWidth = 120;
                    Ext.Msg.alert('提示', '获取表格列配置信息失败！')
                }
            });
            this.setHeaderJson({header: v_colArray});
            // 表格属性
            // this.hiddenColumnCount = 0; //隐藏的列数，默认无隐藏列
        }
        // 将表格列配置转换为columns和fields
        if (v_colArray != null) {
            //循环配置数组，生成fields
            this.setGridFields(v_colArray);
            //循环配置数组，生成columns
            this.columns = this.setGridColumns(v_colArray);
            //如果显示行号,第一列设置为rownumberer
            if (rowNumber.rowNumber) {
                this.columns.unshift({
                    xtype: 'rownumberer',
                    header: rowNumber.header,
                    align: rowNumber.align,
                    width: rowNumber.width
                });
            }
        } else {
            alert("配置源为空！");
        }
    };
    /**
     * 创建grid
     */
    this.create = function () {
        if (this.renderTo == null) {
            //Ext.Msg.alert("提示","未指定容器!");
        }
        // 开启动态加载
        Ext.Loader.setConfig({
            enabled: true
        });
        // 设置扩展插件路径
        //Ext.Loader.setPath('Ext.ux','js/ext5.1/ux');
        Ext.require([
            'Ext.grid.*',
            'Ext.data.*',
            'Ext.util.*',
            'Ext.tip.QuickTipManager',
            'Ext.PagingToolbar'
        ]);
        //读取标签中的ext:qtip属性，并为它赋予显示提示的动作
        Ext.QuickTips.init();
        Ext.tip.QuickTipManager.init();
        // 设置grid配置项
        this.loadProperty();
        // 创建store数据集
        var store = null;
        if (this.data) {
            store = Ext.create('Ext.data.Store', {
                fields: this.fields,
                pageSize: pageConfig.enablePage ? pageConfig.pageSize : 0,
                data: this.data
            });
        } else {
            store = Ext.create('Ext.data.Store', {
                fields: this.fields,
                pageSize: pageConfig.enablePage ? pageConfig.pageSize : 0,
                remoteSort: true,// 后端进行排序
                proxy: {// ajax获取后端数据
                    type: "ajax",
                    method: "POST",
                    url: this.dataUrl,
                    reader: {
                        type: "json",
                        root: "list",
                        totalProperty: "totalcount"
                    },
                    simpleSortMode: true
                }
            });
        }
        //创建导出按钮
        if (exportExcel.exportExcel) {
            var btn_export;
            if (exportExcel.btn != null) {
                //设置导出按钮的id
                exportExcel.id = this.renderTo + "-DSYGrid-btn-export";
                //在导出按钮外加一层div
                btn_export = "<div id='" + exportExcel.id + "'>" + exportExcel.btn + "</div>";
            } else {
                btn_export = Ext.create('Ext.Button', {
                    text: '导出'
                });
                btn_export.setWidth(70);
                //设置导出按钮的id
                exportExcel.id = btn_export.id;
            }
            exportExcel.btn = btn_export;
            this.tbar.push(btn_export);
        }

        //如果分页
        if (pageConfig.enablePage) {
            //创建分页按钮
            pageConfig.pageToolBar = Ext.create("Ext.toolbar.Paging", {
                store: store,
                plugins: [new DSYGrid.component.PagingToolbar({pageConfig: pageConfig})],
                pageSize: pageConfig.pageSize,
                displayInfo: true,
                emptyMsg: "没有数据显示 ",
                afterPageText: '页 共{0}页',
                beforePageText: '第',
                displayMsg: "共{2}条记录",
                firstText: '第一页',
                prevText: '前一页',
                nextText: '后一页',
                lastText: '最后一页',
                refreshText: '刷新',
                border: false
            });
            /*在工具栏添加分页按钮*/
            this.tbar.push("->");
            this.tbar.push(pageConfig.pageToolBar);
        }
        // 设置参数
        store.getProxy().extraParams = this.params;
        var tbar = Ext.create("Ext.Toolbar", {
            height: this.tbarHeight,
            dock: 'top',
            items: this.tbar
        });
        var bbar = Ext.create("Ext.Toolbar", {
            height: this.bbarHeight,
            dock: 'bottom',
            items: this.bbar
        });
        var rbar = Ext.create("Ext.Toolbar", {
            height: this.rbarHeight,
            dock: 'right',
            items: this.rbar
        });
        if (this.rowCss != null) {
            $('<style>.' + this.itemId + '-DSYGrid-row {' + this.rowCss + '}</style>').appendTo(document.body);
        }
        var itemId = this.itemId;
        if (this.checkBox) {
            this.selModel.selType = "checkboxmodel";
        }
        this.grid = Ext.create('Ext.grid.Panel', {
            selModel: this.selModel,
            enableKeyNav: true, //启用键盘导航
            store: store,
            itemId: this.itemId,
            columnLines: this.columnLines,
            columns: this.columns,
            layout: "fit",
            title: this.title,
            border: this.border,
            renderTo: this.renderTo,
            plugins: this.plugins,
            features: this.features,
            viewConfig: {
                stripeRows: true,
                enableTextSelection: true,	//可以复制单元格文字
                columnsText: '显示的列',
                scrollOffset: 30,			// 表格右侧滚动条的宽度，默认是20px
                sortAscText: '升序',
                sortDescText: '降序',
                getRowClass: function () {
                    return itemId + "-DSYGrid-row";
                }
            },
            dockedItems: [
                this.tbar.length > 0 ? tbar : null,
                this.bbar.length > 0 ? bbar : null,
                this.rbar.length > 0 ? rbar : null
            ]
        });
        if (this.width != null) {
            this.grid.setWidth(this.width);
        }
        if (this.height != null) {
            this.grid.setHeight(this.height);
        }
        //配置导出按钮相关参数
        this.exportExcelConfig();
        //给导出按钮绑定事件
        this.exportExcelBind();
        if (this.autoLoad) {
            /* 手动加载store，获取数据 */
            store.load();
        }
        //将本DSYGrid放入管理器
        DSYWidget.Manager.grid[this.itemId] = this;
        return this.grid;
    };
}
/**
 * 通用函数：去掉字符串两端的空格
 */
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};
/**
 * 导出Excel：使用虚拟表单提交
 * @param url
 * @param export_params
 * @param config
 */
function exportExcelFunc(url, export_params, config) {
    if (!document.getElementById("DSYGrid-form-export")) {
        var form = "<form id='DSYGrid-form-export' action='" + url + "' method=post name=form1 style='display:none' target='DSYGrid-iframe-export'>";
        form += "<input type=hidden name=config value='" + config + "'/>";
        //循环参数，生成对应隐藏域提交参数，参数值为数组、对象的，只提交第一层
        for (var name in export_params) {
            if (typeof (export_params[name]) == 'object') {
                for (var i in export_params[name]) {
                    form += "<input type=hidden name=" + name + " value='" + export_params[name][i] + "'/>";
                }
            } else {
                form += "<input type=hidden name=" + name + " value='" + export_params[name] + "'/>";
            }
        }
        form += "</form>";
        //添加iframe防止表单提交后刷新
        form += " <iframe name='DSYGrid-iframe-export' id='DSYGrid-iframe-export' style='display:none'></iframe>";
        $("body").append(form);
        $("#DSYGrid-form-export").submit();
    } else {
        var form = $('#DSYGrid-form-export');
        form.attr('action', url);
        //清空form表单中已有参数
        form.empty();
        form.append("<input type=hidden name=config value='" + config + "'/>");
        //循环参数，生成对应隐藏域提交参数，参数值为数组、对象的，只提交第一层
        for (var name in export_params) {
            if (typeof (export_params[name]) == 'object') {
                for (var i in export_params[name]) {
                    form.append("<input type=hidden name=" + name + " value='" + export_params[name][i] + "'/>");
                }
            } else {
                form.append("<input type=hidden name=" + name + " value='" + export_params[name] + "'/>");
            }
        }
        form.append("<iframe name='DSYGrid-iframe-export' id='DSYGrid-iframe-export' style='display:none'></iframe>");
        form.submit();
    }
}
/**
 * 创建获取表格方法
 */
DSYGrid.getGrid = function (itemId) {
    return Ext.ComponentQuery.query('grid#' + itemId)[0];
};
/**
 * 创建获取导出按钮方法
 */
DSYGrid.getBtnExport = function (itemId) {
    return DSYWidget.Manager.grid[itemId].getExportExcel();
};
/**
 * 创建获取分页按钮方法
 */
DSYGrid.getBtnPage = function (itemId) {
    return DSYWidget.Manager.grid[itemId].getPageConfig().pageToolBar;
};
/**
 * 从DSYGrid管理器中销毁DSYGrid
 * @param itemId
 */
DSYGrid.destoryGrid = function (itemId) {
    if (typeof(DSYGrid.getGrid(itemId)) == "undefined" || DSYGrid.getGrid(itemId) == null) {
        return;
    }
    DSYGrid.getGrid(itemId).close();
    delete DSYWidget.Manager.grid[itemId];
};
/**
 * ext扩展插件：扩展分页按钮组件：每页显示记录数插件
 */
DSYGrid.component.PagingToolbar = Ext.extend(Ext.PagingToolbar, {
    init: function (pagingToolbar) {
        this.pagingToolbar = pagingToolbar;
        //根据每页显示记录数的样式，显示下拉框还是数字匡
        if (this.pageConfig.pageNum && this.pageConfig.pageNumStyle == 'combo') {
            this.inputItem = [
                '&nbsp;&nbsp;&nbsp;每页显示',
                Ext.create('Ext.form.ComboBox', {
                    width: this.pageConfig.pageNumWidth,
                    value: this.pageConfig.pageSize,
                    margin: '0 0 0 -10',
                    allowBlank: false,
                    blankText: '每页显示记录数不允许为空',
                    store: [10, 20, 50, 100],
                    listeners: {
                        scope: this,
                        change: this.onPagingChange
                    }
                }),
                '条&nbsp;&nbsp;&nbsp;'];
        } else if (this.pageConfig.pageNum && this.pageConfig.pageNumStyle == 'numberfield') {
            this.inputItem = [
                '&nbsp;&nbsp;&nbsp;每页显示',
                Ext.create('Ext.form.field.Number', {
                    width: this.pageConfig.pageNumWidth,
                    value: this.pageConfig.pageSize,
                    margin: '0 0 0 -10',
                    allowDecimals: false,
                    allowNegative: false,
                    enableKeyEvents: true,
                    //maxValue: 999,
                    //maxText: '每页不允许超过999条',
                    selectOnFocus: true,
                    submitValue: false,
                    listeners: {
                        scope: this,
                        blur: this.onPagingChange
                    }
                }),
                '条&nbsp;&nbsp;&nbsp;'];
        }
        //将插件插入到分页组件指定位置
        pagingToolbar.insert(this.pageConfig.pageNumIndex, this.inputItem);
    },
    onPagingChange: function (field) {
        //监听事件，刷新表格
        if (field.isValid()) {
            var pageSize = field.getValue();
            if (typeof(pageSize) != 'number') {
                pageSize = Number(pageSize);
                if (isNaN(pageSize)) {
                    Ext.Msg.alert('提示', '每页显示数据数只能是数字类型');
                    return;
                }
            }
            this.pageSize = pageSize;
            this.pageConfig.pageSize = this.pageSize;
            this.pagingToolbar.store.setPageSize(this.pageSize);
            this.pagingToolbar.doRefresh();
        }
    }
});
/**
 * ext扩展插件：表单多选下拉框（带checkbox）
 */
Ext.define('Ext.ux.MultiComboBox', {
    extend: 'Ext.form.ComboBox',
    alias: 'widget.multicombobox',
    initComponent: function () {
        this.multiSelect = true;//设置combobox的多选项为true
        this.editable = false;
        this.listConfig = {
            itemTpl: Ext.create('Ext.XTemplate', '<input type="checkbox">{' + this.displayField + '}'),//为	选项添加checkbox
            //解决问题：选中之后， 下一次点击下拉单的话，会更新 combobox 的下拉选项(combobox), 之前选中的check box 会被取消选择。
            onItemSelect: function (record) {
                var node = this.getNode(record);
                if (node) {
                    Ext.fly(node).addCls(this.selectedItemCls);

                    var checkboxs = node.getElementsByTagName("input");
                    if (checkboxs != null) {
                        var checkbox = checkboxs[0];
                        checkbox.checked = true;
                    }
                }
            },
            listeners: {
                //监听事件：选项选中或取消时，操作对应checkbox
                itemclick: function (view, record, item) {
                    var isSelected = view.isSelected(item);
                    var checkboxs = item.getElementsByTagName("input");
                    if (checkboxs != null) {
                        var checkbox = checkboxs[0];
                        checkbox.checked = !isSelected;
                    }
                }
            }
        };
        this.callParent();
    }
});
